package com.jsmtr.rms.controller.Reports;

import com.jsmtr.rms.entity.BaseConfig;
import com.jsmtr.rms.entity.ProjectInfo;
import com.jsmtr.rms.entity.ProjectInfoFlow;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.service.Report.IReportService;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.ResultBean;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2019/4/3 18:39
 * @modified by:
 */
@Controller
public class ProjectFlowSelectController {

    @Resource
    private IProjectService projectService;
    @Resource
    private IReportService reportService;

    @RequestMapping("/Report/index")
    public String Index(HttpServletRequest request) {

        return "/Report/index";
    }


    @ResponseBody
    @RequestMapping(value = "/Report/getProjectFlowsSelectJson")
    public ResultBean  getProjectFlowsSelectJson(HttpServletRequest request, HttpServletResponse response){
        ResultBean resultBean=new ResultBean();
        List flowsList=new ArrayList();
        EnumTools.RecruitType[] rtyps=  EnumTools.RecruitType.values();
        for (EnumTools.RecruitType rtyp : rtyps) {
            if (rtyp.ordinal()!=0){
                Map<String,Object> flowsData=new HashMap<String,Object>();
                flowsData.put("id",rtyp.ordinal());
                flowsData.put("name",rtyp.name());
                try {
                    List<ProjectInfo> projectInfos=projectService.getProjectInfos(rtyp.ordinal());
                    if (projectInfos!=null&&projectInfos.size()>0){
                       List projectInfoList=new ArrayList();
                        for (ProjectInfo projectInfo : projectInfos) {
                            HashMap<String,Object> projectInfoMap=new HashMap<>();
                            projectInfoMap.put("id",projectInfo.getId());
                            projectInfoMap.put("name",projectInfo.getPname());
                            List<ProjectInfoFlow> flows=projectService.getflowbypid(projectInfo.getId());
                            if (flows!=null&&flows.size()>0){

                                List flowList=new ArrayList();
                                for (ProjectInfoFlow flow : flows) {
                                    HashMap<String,Object> flowMap=new HashMap<>();
                                    String flowName=flow.getFname()==null?flow.getNickname():flow.getFname();
                                    flowMap.put("id",flow.getId());
                                    flowMap.put("name",flowName);
                                    if (flow.getState().equals(0)){
                                        flowMap.put("status",flow.getState());
                                    }
                                    flowList.add(flowMap);
                                }
                                projectInfoMap.put("children",flowList);
                            }
                            projectInfoList.add(projectInfoMap);
                        }
                        flowsData.put("children",projectInfoList);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    resultBean.setMessage("读取招聘方案及流程失败!");
                }
                flowsList.add(flowsData);
            }
        }
        if (flowsList.size()>0){
            resultBean.setMessage("操作成功");
            resultBean.setState(true);
            resultBean.setDatas(flowsList);
        }
        return resultBean;
    }

    @ResponseBody
    @RequestMapping(value = "/Report/getProjectUserByLastFlow")
    public ResultBean getProjectUserByLastFlowId(HttpServletRequest request,HttpServletResponse response){
        ResultBean resultBean=new ResultBean();
        int perpage=1;
        int page=10;
        try {
            perpage = Integer.parseInt(request.getParameter("page"));
            page = Integer.parseInt(request.getParameter("limit"));
          String fid=request.getParameter("fid");
          List<String> fids=new ArrayList<>();
          if(StringTools.trimNotEmpty(fid)){
              fids.add(fid);
          }
       ProjectInfoFlow flow= projectService.getFolwById(fid);
            Map map;
          if ((flow.getPid().toUpperCase().indexOf("C_") >= 0)&& (flow.getPid().toUpperCase().indexOf("E_") >= 0)){
              map=reportService.queryZZResumeAllFlowInfosPage(perpage, page, null, fids, null);
          }else {
            map=  reportService.queryDUserResumeAllFlowInfosPage(perpage, page, null, fids, null);
          }
        if(map!=null) {
            resultBean.setState(true);
            resultBean.setMessage("操作成功");
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");
            resultBean.setPageNumber(String.valueOf(perpage));
            resultBean.setTotalResult(String.valueOf(allcount));
            resultBean.setPageSize(String.valueOf(page));
            resultBean.setTotalPage(String.valueOf(pageCount));
            List<Map> datamap= (List<Map>)map.get("list");
            if (datamap.size()>0){
                for (Map map1 : datamap) {
                    for (Object o : map1.keySet()) {
                        if(map1.get(o)!=null) {
                            map1.put(o, map1.get(o).toString() + "\t");
                        }
                    }
                }
            }
            resultBean.setDatas(map.get("list"));
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultBean;
    }

}
